package com.uchiha.nova.core.publisher;

import com.uchiha.nova.core.event.UsualLogEvent;
import com.uchiha.nova.core.model.LogUsual;
import com.uchiha.nova.core.tool.util.SpringUtil;
import com.uchiha.nova.core.tool.util.WebUtil;

import com.uchiha.nova.core.utils.LogAbstractUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周林静
 * @Date 2021-10-26 8:49
 * @Version 1.0
 */
@Slf4j
public class UsualLogPublisher {

    public static void publishEvent(String level, String id, String data) {
        HttpServletRequest request = WebUtil.getRequest();
        LogUsual logUsual = new LogUsual();
        logUsual.setLogLevel(level);
        logUsual.setLogId(id);
        logUsual.setLogData(data);
        Thread thread = Thread.currentThread();
        StackTraceElement[] trace = thread.getStackTrace();
        if (trace.length > 3) {
            logUsual.setMethodClass(trace[3].getClassName());
            logUsual.setMethodName(trace[3].getMethodName());
        }
        LogAbstractUtil.addRequestInfoToLog(request, logUsual);
        Map<String, Object> event = new HashMap<>(16);
        event.put(EventConstant.EVENT_LOG, logUsual);
        log.info("ErrorLogPublisher:{}",logUsual);
        SpringUtil.publishEvent(new UsualLogEvent(event));
    }

}
